import qs from 'qs'
export default {
    name: 'Notnotified',
    components: {

    },
    data() {
        return {
            handleCardDialog: false,
            givefileData: [],
            height: '600',
            pagesize: 5,
            total: 0,
            currentPage: 1,
            searchForm: {
                deviceId: '',
                unread: false,
                alarmType: ""
            },
            alarmTypeData: [],
            isSelected: false,
            handleEmailDialog: false,
            formLabelWidth: "120px",
            emailForm: {
                email: "",
                remarks: "",
            },
            cardForm: {
                phone: "",
                remarks: "",
            },
            multipleSelection: [],
            unread:true,
            users:"",
            handleupadateDialog:false,
            upadateForm:{
                remarks: "",
                faultId:"",
            },
            formDisabled: false,
        }
    },
    computed: {
        offset: function () {
            return (this.currentPage - 1) * this.pagesize;
        }
    },
    watch: {
        handleEmailDialog: function (oldVal, newVal) {
            if (oldVal) {
                this.emailForm = {
                    email: "",
                    remarks: "",
                }
            }
        },
        handleCardDialog:function(oldVal,newVal){
            if(oldVal){
                this.cardForm = {
                    phone: "",
                    remarks: ""
                }
            }
        }
    },
    created() {
     this.initExceptinolist();
     this.users =JSON.parse(window.localStorage.getItem("userInfo"));
    },
    mounted() {

    },
    methods: {
        //查询未通知的故障信息
        initExceptinolist() {
            var users = JSON.parse(window.localStorage.getItem("userInfo"));
            this.$http.get("/alarmRecord/search?userId="+users.uId+"&limit=" + this.pagesize + "&offset=" + this.offset +
              "&deviceID=" + this.searchForm.deviceId + "&unread=" + this.unread + "&alarmType="+this.searchForm.alarmType)
              .then((res) => {
               if(res.status == 200){
                   this.givefileData = res.data;
                   this.total = res.total;
               }
             }).catch((err) => {
              console.log(err);
          });
        },
        //模糊查询异常信息
        search(){
          this.currentPage = 1;
          this.initExceptinolist();
        },
        //格式化数据
        unreadFormatter(row, cloumn, cellValue, index){
          return cellValue ? '未读' : '已读';
        },
        //发送邮箱
        emailNotifiedClick(){
            if (this.multipleSelection.length == 0) {
                this.$message.error("至少选择一个设备信息");
                return false;
            } else if (this.multipleSelection.length > 1) {
                this.$message.error("只能选择一行数据进行操作");
                return false;
            } else {
                this.handleEmailDialog = true;
            }
        },
        //发送邮箱
        createEmailSure() {
            var emailRule = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/; //邮箱正则
            if (!emailRule.test(this.emailForm.email)) {
                 this.$message.error("邮箱格式错误");
                 return false;
            } else {
                var postData = [];
                this.multipleSelection.forEach(function (val, idx) {
                    postData.push(val.faultId);
                });
                var faultId = postData.toString();
                var postData = {
                    email:this.emailForm.email,
                    remark:this.emailForm.remarks,
                    userId:this.users.uId,
                    faultId:faultId
                }
                this.$http.post('/alarmRecord/emailNotification',qs.stringify(postData))
                 .then((res) => {
                   if(res.status == 200){
                       this.$message({
                           type:"success",
                           message:"邮箱发送成功"
                       });
                       this.handleEmailDialog = false;
                       this.initExceptinolist();
                       return false;
                   }
                   this.$message.error(res.message);
                 }).catch((err) => {
                   console.log(err);
               });
            }
        },
        //发送短信
        cardNotifiedClick(){
            if (this.multipleSelection.length == 0) {
                this.$message.error("至少选择一个设备信息");
                return false;
            } else if (this.multipleSelection.length > 1) {
                this.$message.error("只能选择一行数据进行操作");
                return false;
            } else {
                var postData = [];
                this.multipleSelection.forEach(function (val, idx) {
                    postData.push(val.faultId);
                });
                var faultId = postData.toString();
                this.cardForm.faultId = faultId;
                this.handleCardDialog = true;
            }
        },
        //发送短信
        createSureCard() {
            var telRule = /^1[0-9]{10}$/; //电话正则
            if (!telRule.test(this.cardForm.phone)) {
                this.$message.error("手机号码格式错误");
                return false;
            }else{
                var postData = [];
                this.multipleSelection.forEach(function (val, idx) {
                    postData.push(val.faultId);
                });
                var faultId = postData.toString();
                var postData = {
                    phone:this.cardForm.phone,
                    remark:this.cardForm.remarks,
                    userId:this.users.uId,
                    faultId:faultId
                }
                this.$http.post('/alarmRecord/shortNoteNotification',qs.stringify(postData))
                 .then((res) => {
                   if(res.status == 200){
                      this.$message({
                          type:"success",
                          message:"发送成功"
                      });
                      this.handleCardDialog = false;
                      this.initExceptinolist();
                      return false;
                   }
                   this.$message.error(res.message);
                 }).catch((err) => {
                console.log(err);
             });
           }
        },
        //批量已读
        readClick(){
            if (this.multipleSelection.length < 1) {
                this.$message.error("请选择要已读的故障编号");
                return false;
            }
            var postData = [];
            this.multipleSelection.forEach(function (val, idx) {
                postData.push(val.faultId);
            });
            var txtName = postData.join(",");
            console.log(txtName);
            this.$confirm('是否将[' + txtName + ']已读,确定继续？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http.post('/alarmRecord/updateStatus?faultIds=' + txtName)
                    .then((res) => {
                        if (res.status == 200) {
                            this.$message({
                                type: 'success',
                                message: "修改成功"
                            });
                            this.initExceptinolist();
                            return false;
                        }
                        this.$message.error("修改失败");
                    }).catch((err) => {
                    console.log(err);
                });
            });
        },
        rowClick(row){
          this.upadateForm.faultId = row.faultId;
          this.upadateForm.remarks = row.remark;
          this.formDisabled = true;
          this.handleupadateDialog = true;
        },
        //修改报警备注
        modifyRemarks(){
          var postData = {
              faultId :this.upadateForm.faultId,
              remark:this.upadateForm.remarks,
          }
          this.$http.post("/alarmRecord/updateRemark",qs.stringify(postData))
           .then((res) => {
             if(res.status == 200){
                 this.$message({
                     type:"success",
                     message:"修改成功"
                 });
                 this.handleupadateDialog = false;
                 this.initExceptinolist();
                 return;
              }
              this.$message.error("修改失败");
            }).catch((err) => {
             console.log(err);
          });
        },
        toggleSelection(rows) {
            if (rows) {
                rows.forEach(row => {
                    this.$refs.multipleTable.toggleSelection(row);
                });
            } else {
                this.$refs.multipleTable.clearSelection();
          }
        },
        handleSelectionChange(val) {
          this.multipleSelection = val;
        },
        handleSizeChange(val) {
            this.pagesize = val;
            this.initExceptinolist();
        },
        handleCurrentChange(val) {
            this.currentPage = val;
            this.initExceptinolist();
        }
    },
}